package it.fuesi.expired.adapters;

import android.app.Fragment;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Date;

import it.fuesi.expired.R;
import it.fuesi.expired.api.client.AttachmentsBroker;
import it.fuesi.expired.api.client.ItemsBroker;
import it.fuesi.expired.model.Attachment;
import it.fuesi.expired.model.Item;
import it.fuesi.expired.model.collections.ItemCollection;

/**
 * Created by pascotto on 22/08/14.
 */
public class ItemsAdapter extends AbstractAdapter<Item,ItemCollection> {



    private ImageView mPicture;


    public ItemsAdapter(LayoutInflater inflater, ItemCollection data, Context context, Fragment fragment)  {
        super(inflater, data, context, fragment);
    }

    @Override
    protected void drawView(View view, Item item) {
        final TextView name = (TextView)view.findViewById(R.id.item_name);
        name.setText(item.getName());

        final TextView expirationDate = (TextView)view.findViewById(R.id.item_expire_date);
        expirationDate.setText(item.getExpireDate(getContext().getString(R.string.date_format)));

        ImageView mAlert = (ImageView)view.findViewById(R.id.item_alert_icon);

        if(item.getExpireDate().compareTo(new Date()) <=0 )
            mAlert.setVisibility(View.VISIBLE);
        else
            mAlert.setVisibility(View.INVISIBLE);

        Log.d(TAG, "Item " + item.getName() + " has picture: " + (item.getAttachmentId() != null));

        if(item.getAttachmentId() != null){
            mPicture = (ImageView)view.findViewById(R.id.item_icon);
            new GetAttachmentTask().execute(item.getAttachmentId());
        }

    }

    @Override
    protected int getListItemLayout(){
        return R.layout.item;
    }

    @Override
    protected ItemsBroker getClientBroker() {
        return new ItemsBroker(API_ENDPOINT);
    }

    @Override
    protected boolean matchCurrentFilter(Item obj, CharSequence searchText) {
        return searchText == null || obj.getName().toUpperCase().contains(searchText.toString().toUpperCase());
    }


    private class GetAttachmentTask extends AsyncTask<String, Void, byte[]> {

        @Override
        protected byte[] doInBackground(String... params) {
            try {

                String _id;
                if(params != null && params.length > 0)
                    _id = params[0];
                else
                    _id = null;


                Log.d(TAG, "Retrieving picture with _id: " + _id);

                AttachmentsBroker broker = new AttachmentsBroker(AbstractAdapter.API_ENDPOINT)  ;
                byte[] b =  broker.getFile(_id);
                Log.d(TAG, "Byte array is not null: " + (b != null));
                return b;

            } catch (Exception e) {
                Log.e("SaveAttachmentTask", e.getMessage(), e);
            }

            return null;
        }

        @Override
        protected void onPostExecute(byte[] obj) {

            Log.d("SaveAttachmentTask", "OnPostExecute:" + obj);

            if(obj != null){
                Log.d(TAG, "Picture retrieved, setting to imageView: ");
                mPicture.setImageDrawable(null);
                mPicture.setImageBitmap(BitmapFactory.decodeByteArray(obj, 0, obj.length));
            }

        }

    }



}
